package com.cskaoyan.gateway.controller.shopping;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.ICartService;
import com.mall.shopping.dto.*;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.intercepter.TokenIntercepter;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("shopping/carts")
public class CartsController {

    @Reference(check = false)
    ICartService iCartService;

    @PutMapping()
    public ResponseData<String> updateCartNumber(@RequestBody UpdateCartNumRequest request) {
        if (!("true".equals(request.getChecked()) || "false".equals(request.getChecked()) || "1".equals(request.getChecked()))) {
            return new ResponseUtil<String>().setErrorMsg(SysRetCodeConstants.REQUISITE_PARAMETER_NOT_EXIST.getMessage());
        }
        UpdateCartNumResponse response = iCartService.updateCartNum(request);
        return SysRetCodeConstants.SUCCESS.getCode().equals(response.getCode())
                ? new ResponseUtil<String>().setData(SysRetCodeConstants.SUCCESS.getMessage())
                : new ResponseUtil<String>().setErrorMsg(response.getMsg());
    }

    @GetMapping
    public ResponseData getShoppingCarts(HttpServletRequest request){
        String userInfo = (String) request.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());
        CartListByIdRequest idRequest = new CartListByIdRequest();
        idRequest.setUserId(uid);
        CartListByIdResponse response = iCartService.getCartListById(idRequest);
        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getCartProductDtos());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    @PostMapping
    public ResponseData addShoppingCarts(@RequestBody AddCartsRequest request){
        AddCartRequest addCartRequest = new AddCartRequest();
        addCartRequest.setUserId(request.getUserId());
        addCartRequest.setItemId(request.getProductId());
        addCartRequest.setNum(request.getProductNum());
        AddCartResponse response = iCartService.addToCart(addCartRequest);
        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getMsg());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    @DeleteMapping("{uid}/{pid}")
    public ResponseData deleteCarts(@PathVariable("uid") Long uid,@PathVariable("pid") Long pid){
        DeleteCartItemRequest request = new DeleteCartItemRequest();
        request.setUserId(uid);
        request.setItemId(pid);
        DeleteCartItemResponse response = iCartService.deleteCartItem(request);
        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getMsg());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }
}
